﻿$(document).ready(function() {
    $("#NickName").change(function() {
        $("#output").load("/Content/Static/loading.image");
        var name = $("#NickName").val();
        $.post("/Search/SearchForNickname", { 'nick': name }, function(data) {
            if (data == "true") {
                $("#NickValidation").load("/Content/Static/unavailable.message");
            }
            else {
                $("#NickValidation").load("/Content/Static/available.message");
                $("#Password").focus();
            }
        });
        return false;
    });

    var formOptions = {
        beforeSubmit: showRequest,
        success: showResponse,
        clearForm: false,
        resetForm: false
    };

    $("#registrationForm").ajaxForm(formOptions);

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 100,
        width: 300,
        zIndex: 400,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });
});

function clearRegistrationForm() {
    setTexBoxtData("FirstName", '');
    setTexBoxtData("LastName", '');
    setTexBoxtData("EMail", '');
    setTexBoxtData("NickName", '');
    setTexBoxtData("Password", '');
    setTexBoxtData("ConfirmPassword", '');
}

function showRequest(formData, jqForm, options) {
    var errorCount = 0;
    var form = jqForm[0];

    if (!form.FirstName.value) {
        setLabelData("firstNameError", 'Please enter a first name.');
        errorCount++;
    }
    else {
        setLabelData("firstNameError", '');
    }

    if (!form.LastName.value) {
        setLabelData("lastNameError", 'Please enter a last name.');
        errorCount++;
    }
    else {
        setLabelData("lastNameError", '');
    }

    if (!form.EMail.value) {
        setLabelData("emailError", 'Please enter an email address.');
        errorCount++;
    }
    else {
        setLabelData("emailError", '');
    }

    if (!form.NickName.value) {
        setLabelData("nickError", 'Please enter a nickname.');
        errorCount++;
    }
    else {
        setLabelData("nickError", '');
    }

    if (!form.Password.value) {
        setLabelData("passwordError", 'Please enter a password.');
        errorCount++;
    }
    else {
        setLabelData("passwordError", '');
    }

    if (!form.ConfirmPassword.value) {
        setLabelData("confirmPasswordError", 'Please confirm your password.');
        errorCount++;
    }
    else {
        setLabelData("confirmPasswordError", '');
    }

    if (form.ConfirmPassword.value != form.Password.value) {
        setLabelData("confirmPasswordError", 'Your passwords did not match, please try again.');
        errorCount++;
    }
    else {
        setLabelData("confirmPasswordError", '');
    }

    return errorCount == 0;
}

function showResponse(responseText, statusText, xhr, $form) {
    if (responseText == "Successful") {
        $("#dialogContent").load("/Content/Static/successregistration.message");
        $("#NickValidation").html('');
        clearRegistrationForm();
    } else {
        $("#dialogContent").html(responseText);
    }
    $("#dialog").dialog('open');
}